//
//  ViewController.m
//  自定义高度
//
//  Created by 逗比小胖！ on 16/2/29.
//  Copyright © 2016年 徐晨. All rights reserved.
//

#import "FriendViewController.h"

#import "Friend.h"
#import "FriendFrame.h"
#import "FriendTableViewCell.h"
#import "CommentViewController.h"


@interface FriendViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)NSArray *friendFrameArray;

@property(nonatomic,weak)UITableView *tableView;

@end

@implementation FriendViewController

- (NSArray *)friendFrameArray {
    if (_friendFrameArray == nil) {
        _friendFrameArray = [Friend friendFrameArray];
    }
    //NSLog(@"%@",_weiBoFrameArray);
    
    return _friendFrameArray;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self tableView];
    // Do any additional setup after loading the view, typically from a nib.
}
- (UITableView *)tableView {
    if (_tableView == nil) {
        UITableView *tempTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
        _tableView = tempTableView;
        //设置代理和资源来源
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        //将tempTableViewh加入主view
        [self.view addSubview:_tableView];
    }
    
    return _tableView;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.friendFrameArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FriendTableViewCell *cell = [FriendTableViewCell friendcellWithTableView:tableView];
    cell.friendFrame = self.friendFrameArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CommentViewController *commentContro = [[CommentViewController alloc]initWithNibName:@"CommentViewController" bundle:nil];
    NSLog(@"999999999999999999999999%@",self.friendFrameArray[indexPath.row]);
    __weak typeof(self) tempSelf = self;
    commentContro.commentFriendBlock = ^Friend *{
        
        FriendFrame *tempFrame = tempSelf.friendFrameArray[indexPath.row];
        return  tempFrame.friend;
    };
    [self presentViewController:commentContro animated:YES completion:nil];
}

//设置cell的行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    FriendFrame *friendFrame = self.friendFrameArray[indexPath.row];
    return friendFrame.cellHeight;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
